<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Pelajaran extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_pelajaran';

		$this->primary_key = 'aka_pelajaran.pelajaran_id';

	}
	function get_all($kategori_rapor_id,$aktif=null){
		$this->load->database();	
			$filter = "";
			
			if($aktif != null)
				$filter = "AND {$this->table_name}.aktif='$aktif'";
			
			$sql = "SELECT aka_pelajaran.*,aka_kategori_rapor.nama_rapor  FROM aka_pelajaran LEFT JOIN aka_kategori_rapor ON aka_pelajaran.kategori_rapor_id=aka_kategori_rapor.kategori_rapor_id WHERE aka_pelajaran.kategori_rapor_id='$kategori_rapor_id' $filter order by {$this->primary_key} ";	

			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_pelajaran(){
		$this->load->database();
			
			$sql = "SELECT * from aka_pelajaran ORDER BY aka_pelajaran.pelajaran_id ";
		
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_by($id){
		$this->load->database();
			
			$sql = "SELECT aka_pelajaran.*,aka_kategori_rapor.nama_rapor  FROM aka_pelajaran LEFT JOIN aka_kategori_rapor ON aka_pelajaran.kategori_rapor_id=aka_kategori_rapor.kategori_rapor_id WHERE aka_pelajaran.pelajaran_id='$id' ";
		/*	
		$sql = "SELECT * FROM {$this->table_name} WHERE {$this->primary_key}='$id';";
		*/
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where($this->primary_key,$id);
				$this->db->update($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM {$this->table_name} WHERE {$this->primary_key}='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>